using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Collections;

namespace 音乐播放盒
{    
    public partial class 主窗口 : Form
    {
        private const int WM_VSCROLL = 0x115;
        // Scroll Bar Constants
        private const int SB_HORZ = 0;
        private const int SB_VERT = 1;
        private const int SB_CTL = 2;
        private const int SB_BOTH = 3;

        // Scroll Bar Commands
        private const int SB_LINEUP = 0;
        private const int SB_LINELEFT = 0;
        private const int SB_LINEDOWN = 1;
        private const int SB_LINERIGHT = 1;
        private const int SB_PAGEUP = 2;
        private const int SB_PAGELEFT = 2;
        private const int SB_PAGEDOWN = 3;
        private const int SB_PAGERIGHT = 3;
        private const int SB_THUMBPOSITION = 4;
        private const int SB_THUMBTRACK = 5;
        private const int SB_TOP = 6;
        private const int SB_LEFT = 6;
        private const int SB_BOTTOM = 7;
        private const int SB_RIGHT = 7;
        private const int SB_ENDSCROLL = 8;
        private const int WM_PAINT = 0x000F;


        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool ScrollWindow(IntPtr hWnd, int XAmount, int YAmount, ref Rectangle lpRect, ref Rectangle lpClipRect);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern int SetScrollPos(IntPtr hwnd, int nBar, int nPos, bool bRedraw);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern int SetScrollPos(int nBar, int nPos, bool bRedraw);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern int GetScrollPos(IntPtr hwnd, int nBar);


        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern bool UpdateWindow(IntPtr hWnd);

        [System.Runtime.InteropServices.DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);


        private void SelfSrcoll(IntPtr handle)
        {
            SendMessage(handle, WM_VSCROLL, SB_THUMBTRACK, 100);
            UpdateWindow(handle);
        }
        private int pre_width = 500;    //播放器主窗口的宽度
        private int pre_height = 400;
        private int now_music_id = -1;  //当前播放歌曲的序号，-1表示没在播放
        private bool isClose = false;    //是否退出程序
        private bool first_in = true;   //是否第一次进入歌词区域
        ArrayList al = new ArrayList(); //当前歌词时间表     
        public 主窗口()
        {
            InitializeComponent();
        }

        private void 主窗口_Resize(object sender, EventArgs e)
        {
            //由于这2个窗口都是由主窗口创建的，所以，主窗口最小化时，他们都最小化了，这段代码就是为了防止桌面歌词被最小化看不见
            if (this.WindowState == FormWindowState.Minimized)
            {
                if (this.lrcForm != null)
                {
                    this.tmForm.Hide();
                    this.lrcForm.Show();                    
                }
            }

            Form f = (Form)sender;

            if (f.Width < 400)
                return;

            /*下面这段代码是播放器主窗口里的各个控件自适应主窗口改变的代码*/
            this.button1.Location = new Point((int)(f.Width / 2 - 25 - 69), this.button1.Location.Y);
            this.button2.Location = new Point((int)(f.Width / 2 - 25), this.button2.Location.Y);
            this.button3.Location = new Point((int)(f.Width / 2 + 25 + 9), this.button3.Location.Y);
            this.panel1.Width += f.Width - this.pre_width; ;
            this.trackBar1.Width += f.Width - this.pre_width;
            this.label2.Location = new Point(this.label2.Location.X + f.Width - this.pre_width, this.label2.Location.Y);
            this.label3.Location = new Point((int)(this.panel1.Width / 2 - this.label3.Width / 2), this.label3.Location.Y);
            this.pre_width = f.Width;
            this.pre_height = f.Height;            
            
        }

        /// <summary>
        /// 添加歌曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            songSrcChoice = new 歌曲来源选择();
            this.songSrcChoice.Owner = this;
            songSrcChoice.ShowDialog();            
            //return;

            
        }

        /// <summary>
        /// 删除歌曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button5_Click(object sender, EventArgs e)
        {

            //MessageBox.Show("删除功能还不完善，暂时屏蔽掉了");     
            /*删除功能已经完善*/
            if (this.listView1.SelectedItems.Count == 0)
            {
                MessageBox.Show("无法删除，请先选定要删除的歌曲");
                return;
            }

            //MessageBox.Show(this.listView1.SelectedItems.Count.ToString());

            ListView.SelectedListViewItemCollection slvc = this.listView1.SelectedItems;
            
            int x = -1;
            if (this.now_music_id >= 0)
            {
                x = slvc.IndexOf(this.listView1.Items[this.now_music_id]);
            }

            if (x >= 0)
            {
                this.button2.Text = "播放";
                this.label3.Text = "无";
                this.label1.Text = "00:00";
                this.label2.Text = "00:00";
                this.timer1.Enabled = false;
                this.Mp3Player.Ctlcontrols.stop();
                this.trackBar1.Value = 0;
                this.now_music_id = -1;
            }

            XmlDocument xd = new XmlDocument();
            xd.Load(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");

            foreach (ListViewItem lvi in slvc)
            {
                XmlNode xn = xd.DocumentElement.ChildNodes[lvi.Index];
                xd.DocumentElement.RemoveChild(xn);
                this.listView1.Items.Remove(lvi);
            }

            for (int i = 0; i < this.listView1.Items.Count; i++) {
                if (this.now_music_id == Convert.ToInt32(this.listView1.Items[i].SubItems[0].Text) - 1)
                {
                    this.now_music_id = i;
                }
                this.listView1.Items[i].SubItems[0].Text = (i + 1).ToString();
            }

            xd.Save(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
        }

        private void 主窗口_Load(object sender, EventArgs e)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");

            int i = 1;
            foreach (XmlNode xn in xd.DocumentElement.ChildNodes)
            {
                string[] t = { i.ToString(), Path.GetFileNameWithoutExtension(xn.InnerText) };
                ListViewItem lvi = new ListViewItem(t);
                this.listView1.Items.Add(lvi);
                i++;
            }
            this.comboBox1.SelectedIndex = 0;
        }

        /// <summary>
        /// 播放或暂停歌曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            if (this.now_music_id == -1)
                return;

            if (this.button2.Text == "播放" && this.trackBar1.Value < (this.trackBar1.Maximum - 2))
            {
                this.timer1.Enabled = true;
                this.Mp3Player.Ctlcontrols.play();
                
                this.button2.Text = "暂停";
            }
            else if (this.button2.Text == "播放" && this.trackBar1.Value >= (this.trackBar1.Maximum - 2))
            {
                this.timer1.Enabled = true;
                this.button2.Text = "暂停";
                this.PlayModeChange();
            }
            else 
            {
                this.timer1.Enabled = false;
                this.Mp3Player.Ctlcontrols.pause();               
                this.button2.Text = "播放";
                            
            }
        }

        /// <summary>
        /// 双击播放歌曲
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (this.listView1.SelectedItems.Count == 0)
                return;
            
            this.button2.Text = "暂停";
            this.timer1.Enabled = true;
            ListViewItem lvi = this.listView1.SelectedItems[0];

            //MessageBox.Show("双击");
            this.label3.Text = this.listView1.SelectedItems[0].SubItems[1].Text;
        
            this.label3.Text = lvi.SubItems[1].Text;
            if (this.label3.Text.Length > 15)
                this.label3.Text = this.label3.Text.Substring(0, 13) + "..";
            this.label3.Location = new Point((int)(this.panel1.Width / 2 - this.label3.Width / 2), this.label3.Location.Y);

            this.now_music_id = Convert.ToInt32(lvi.SubItems[0].Text) - 1;
            XmlDocument xd = new XmlDocument();
            xd.Load(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
            string filename = xd.DocumentElement.ChildNodes[this.now_music_id].InnerText;
            //MessageBox.Show(filename);    //用于测试
            this.musicPlay(filename);
            string lrc_filename = Path.GetDirectoryName(filename) + "\\" + Path.GetFileNameWithoutExtension(filename) + ".lrc";
            if (File.Exists(lrc_filename))
            {
                loadLrc(lrc_filename);    
            }
            else
            {
                this.richTextBox1.Text = "歌词文件不存在";
            }
        }

        /// <summary>
        /// 播放音乐
        /// </summary>
        /// <param name="filename"></param>
        protected void musicPlay(string filename)
        {
            this.Mp3Player.Ctlcontrols.stop();
            this.Mp3Player.URL = filename;
            this.Mp3Player.Ctlcontrols.play();
            this.label1.Text = this.Mp3Player.Ctlcontrols.currentPositionString;
            this.label2.Text = this.Mp3Player.currentMedia.durationString;
            this.trackBar1.Maximum = Convert.ToInt32(this.Mp3Player.currentMedia.duration);
            this.trackBar1.Value = Convert.ToInt32(this.Mp3Player.Ctlcontrols.currentPosition);
        }

        private void Mp3Player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
        {
            
        }

        /// <summary>
        /// 设置音量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            if (this.now_music_id == -1)
                return;

            this.Mp3Player.settings.volume = this.trackBar2.Value;
        }

        /// <summary>
        /// 定时器执行的方法，每隔1秒执行一次
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {           
            this.label1.Text = this.Mp3Player.Ctlcontrols.currentPositionString;
            this.label2.Text = this.Mp3Player.currentMedia.durationString;
            this.trackBar1.Maximum = Convert.ToInt32(this.Mp3Player.currentMedia.duration);
            this.trackBar1.Value = Convert.ToInt32(this.Mp3Player.Ctlcontrols.currentPosition);

            if (this.richTextBox1.Text != "歌词文件不存在" && this.richTextBox1.Text != "歌词文件内容为空")
            {
                int pos = al.IndexOf(this.trackBar1.Value.ToString());
                bool isAr = this.richTextBox1.Text.Contains("歌手:");
                bool isTi = this.richTextBox1.Text.Contains("歌名:");


                if (pos >= 0)
                {
                    int n = isAr ? 1 : 0;
                    int m = isTi ? 1 : 0;

                    int height = 28 * (this.al.Count + m + n);
                    int max = height - this.richTextBox1.Height;


                    this.richTextBox1.SelectAll();
                    this.richTextBox1.SelectionColor = Color.Black;
                    this.richTextBox1.SelectionLength = 0;/**/

                    int l = this.richTextBox1.Lines[pos + m + n].Length;
                    this.richTextBox1.Select(this.richTextBox1.GetFirstCharIndexFromLine(pos + m + n), l);
                    this.richTextBox1.SelectionColor = Color.OrangeRed;
                    this.richTextBox1.SelectionLength = 0;
                    //this.Text = GetScrollPos(this.richTextBox1.Handle, SB_VERT).ToString() + "-" + al.Count + "-" + this.richTextBox1.Height;

                    if ((pos + m + n) * 28 <= max)
                    {
                        int start = this.richTextBox1.GetFirstCharIndexFromLine(pos + m + n);
                        this.richTextBox1.SelectionStart = start;
                        this.richTextBox1.ScrollToCaret();
                        
                    }
                    else {
                        //this.richTextBox1.Focus();
                        SendMessage(this.richTextBox1.Handle, WM_VSCROLL, SB_BOTTOM, 0);
                        UpdateWindow(this.richTextBox1.Handle);
                        //this.richTextBox1.SelectionStart = this.richTextBox1.Text.Length;
                        //this.richTextBox1.ScrollToCaret();
                    }

                    if (this.lrcForm != null)
                    {
                        string l1 = this.richTextBox1.Lines[pos + m + n];
                        string l2;
                        if ((pos + m + n) < this.richTextBox1.Lines.Length - 1)
                        {
                            l2 = this.richTextBox1.Lines[pos + m + n + 1];
                        }
                        else {
                            l2 = "。。。。。";
                        }                        

                        this.lrcForm.setLrc(l1, l2, pos);
                        //this.lrcForm.setLrc(ArrayList al,);
                    
                    }

                }
            }   
  
            //this.Text = this.trackBar1.Value.ToString();

            if (this.trackBar1.Value >= (this.trackBar1.Maximum - 2))
            {
                this.PlayModeChange();
            }               
            
        }

        /// <summary>
        /// 歌曲进度条滚变化事件处理函数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            
            if (this.now_music_id == -1)
               return;

            this.Mp3Player.Ctlcontrols.currentPosition = this.trackBar1.Value;
            this.label1.Text = this.Mp3Player.Ctlcontrols.currentPositionString;

            //this.Text = this.trackBar1.Value.ToString();

            if (this.trackBar1.Value >= (this.trackBar1.Maximum - 2) && this.button2.Text == "暂停")
            {
                this.PlayModeChange();
            }
            
        }

        /// <summary>
        /// 播放模式切换
        /// </summary>
        public void PlayModeChange() 
        {           
            switch (this.comboBox1.SelectedItem.ToString())
            {
                case "单曲循环":
                    this.Mp3Player.Ctlcontrols.stop();
                    this.Mp3Player.Ctlcontrols.play();
                    break;
                case "顺序播放":
                    if (this.now_music_id + 1 == this.listView1.Items.Count)
                    {
                        this.Mp3Player.Ctlcontrols.stop();
                        this.button2.Text = "播放";
                        this.timer1.Enabled = false;
                        this.trackBar1.Value = 0;
                        this.label1.Text = "00:00";
                        //this.richTextBox1.Text = "";
                        break;
                    }
                    this.now_music_id += 1;
                    XmlDocument xd1 = new XmlDocument();
                    xd1.Load(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
                    string filename1 = xd1.DocumentElement.ChildNodes[this.now_music_id].InnerText;
                    this.musicPlay(filename1);
                    string lrc_filename1 = Path.GetDirectoryName(filename1) + "\\" + Path.GetFileNameWithoutExtension(filename1) + ".lrc";
                    if (File.Exists(lrc_filename1))
                    {
                        loadLrc(lrc_filename1);
                    }
                    else
                    {
                        this.richTextBox1.Text = "歌词文件不存在";
                    }
                    this.label3.Text = this.listView1.Items[this.now_music_id].SubItems[1].Text;
                    if (this.label3.Text.Length > 15)
                        this.label3.Text = this.label3.Text.Substring(0, 13) + "..";
                    this.label3.Location = new Point((int)(this.panel1.Width / 2 - this.label3.Width / 2), this.label3.Location.Y);
                    break;
                case "循环播放":
                    this.now_music_id = this.now_music_id + 1 == this.listView1.Items.Count ? 0 : this.now_music_id + 1;
                    XmlDocument xd2 = new XmlDocument();
                    xd2.Load(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
                    string filename2 = xd2.DocumentElement.ChildNodes[this.now_music_id].InnerText;
                    this.musicPlay(filename2);
                    string lrc_filename2 = Path.GetDirectoryName(filename2) + "\\" + Path.GetFileNameWithoutExtension(filename2) + ".lrc";
                    if (File.Exists(lrc_filename2))
                    {
                        loadLrc(lrc_filename2);
                    }
                    else
                    {
                        this.richTextBox1.Text = "歌词文件不存在";
                    }
                    this.label3.Text = this.listView1.Items[this.now_music_id].SubItems[1].Text;
                    if (this.label3.Text.Length > 15)
                        this.label3.Text = this.label3.Text.Substring(0, 13) + "..";
                    this.label3.Location = new Point((int)(this.panel1.Width / 2 - this.label3.Width / 2), this.label3.Location.Y);
                    break;
                case "随机播放":
                    Random rd = new Random();
                    this.now_music_id = rd.Next(this.listView1.Items.Count);
                    XmlDocument xd3 = new XmlDocument();
                    xd3.Load(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
                    string filename3 = xd3.DocumentElement.ChildNodes[this.now_music_id].InnerText;
                    this.musicPlay(filename3);
                    string lrc_filename3 = Path.GetDirectoryName(filename3) + "\\" + Path.GetFileNameWithoutExtension(filename3) + ".lrc";
                    if (File.Exists(lrc_filename3))
                    {
                        loadLrc(lrc_filename3);
                    }
                    else
                    {
                        this.richTextBox1.Text = "歌词文件不存在";
                    }
                    this.label3.Text = this.listView1.Items[this.now_music_id].SubItems[1].Text;
                    if (this.label3.Text.Length > 15)
                        this.label3.Text = this.label3.Text.Substring(0, 13) + "..";
                    this.label3.Location = new Point((int)(this.panel1.Width / 2 - this.label3.Width / 2), this.label3.Location.Y);
                    break;
                default:
                    break;
            }
            foreach (ListViewItem lvi in this.listView1.Items)
            {
                lvi.Selected = false;
            }
            this.listView1.Items[this.now_music_id].Selected = true;
            this.listView1.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.now_music_id == -1)
                return;

            this.now_music_id = this.now_music_id == 0 ? this.listView1.Items.Count - 1 : this.now_music_id - 1;
            foreach (ListViewItem lvi in this.listView1.Items)
            {
                lvi.Selected = false;
            }
            this.listView1.Items[this.now_music_id].Selected = true;
            this.listView1.Focus();
            XmlDocument xd = new XmlDocument();
            xd.Load(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
            string filename = xd.DocumentElement.ChildNodes[this.now_music_id].InnerText;
            this.musicPlay(filename);
            string lrc_filename = Path.GetDirectoryName(filename) + "\\" + Path.GetFileNameWithoutExtension(filename) + ".lrc";
            if (File.Exists(lrc_filename))
            {
                loadLrc(lrc_filename);
            }
            else
            {
                this.richTextBox1.Text = "歌词文件不存在";
            }
            this.button2.Text = "暂停";
            this.timer1.Enabled = true;
            this.label3.Text = this.listView1.Items[this.now_music_id].SubItems[1].Text;
            if (this.label3.Text.Length > 15)
                this.label3.Text = this.label3.Text.Substring(0, 13) + "..";
            this.label3.Location = new Point((int)(this.panel1.Width / 2 - this.label3.Width / 2), this.label3.Location.Y);                        
        }

        /// 切换到下一曲
        private void button3_Click(object sender, EventArgs e)
        {
            if (this.now_music_id == -1)
                return;

            this.now_music_id = this.now_music_id == this.listView1.Items.Count - 1 ? 0 : this.now_music_id + 1;
            foreach (ListViewItem lvi in this.listView1.Items)
            {
                lvi.Selected = false;
            }
            this.listView1.Items[this.now_music_id].Selected = true;
            this.listView1.Focus();
            XmlDocument xd = new XmlDocument();
            xd.Load(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
            string filename = xd.DocumentElement.ChildNodes[this.now_music_id].InnerText;
            this.musicPlay(filename);
            string lrc_filename = Path.GetDirectoryName(filename) + "\\" + Path.GetFileNameWithoutExtension(filename) + ".lrc";
            if (File.Exists(lrc_filename))
            {
                loadLrc(lrc_filename);
            }
            else
            {
                this.richTextBox1.Text = "歌词文件不存在";
            }
            this.button2.Text = "暂停";
            this.timer1.Enabled = true;
            this.label3.Text = this.listView1.Items[this.now_music_id].SubItems[1].Text;
            if (this.label3.Text.Length > 15)
                this.label3.Text = this.label3.Text.Substring(0, 13) + "..";
            this.label3.Location = new Point((int)(this.panel1.Width / 2 - this.label3.Width / 2), this.label3.Location.Y);                        
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.关于ToolStripMenuItem.Enabled == true)
                this.关于ToolStripMenuItem.Enabled = false;

            DialogResult dr = MessageBox.Show("这什么啊");

            if (dr == DialogResult.OK)
                this.关于ToolStripMenuItem.Enabled = true;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.isClose = true;
            this.Dispose();
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.Visible)
                {
                    this.Hide();
                }
                else
                {                    
                    this.Show();
                    this.WindowState = FormWindowState.Normal;
                }
            }
        }


        /// 载入歌词

        public void loadLrc(string lrc_filename) 
        {
            using (StreamReader sr = new StreamReader(new FileStream(lrc_filename, FileMode.Open), Encoding.Default))
            {
                string tempLrc = "";
                while (!sr.EndOfStream)
                {
                    tempLrc = sr.ReadToEnd();
                }

                if(tempLrc.Trim() == "")
                {
                    this.richTextBox1.Text = "歌词文件内容为空";
                    return;
                }

                tempLrc = tempLrc.Trim();    
                //this.richTextBox1.Text = sr.ReadToEnd();
                Regex rg = new Regex("\r*\n*\\[ver:(.*)\\]\r*\n*");                
                tempLrc = rg.Replace(tempLrc, "");
                rg = new Regex("\r*\n*\\[al:(.*)\\]\r*\n*");
                tempLrc = rg.Replace(tempLrc, "");
                rg = new Regex("\r*\n*\\[by:(.*)\\]\r*\n*");
                tempLrc = rg.Replace(tempLrc, "");
                rg = new Regex("\r*\n*\\[offset:(.*)\\]\r*\n*");
                tempLrc = rg.Replace(tempLrc, "");
                rg = new Regex("\r*\n*\\[ar:(.*)\\]\r*\n*");
                Match mtch;
                mtch = rg.Match(tempLrc);
                tempLrc = rg.Replace(tempLrc, "\n歌手:" + mtch.Groups[1].Value + "\n");
                rg = new Regex("\r*\n*\\[ti:(.+?)\\]\r*\n*");   
                mtch = rg.Match(tempLrc);            
                //int i = mtch.Groups.Count;
                tempLrc = rg.Replace(tempLrc, "\n歌名:" + mtch.Groups[1].Value + "\n");
                rg = new Regex("\r*\n*\\[[0-9][0-9]:[0-9][0-9].[0-9][0-9]\\]");
                //int line = rg.Matches(tempLrc).Count;
                MatchCollection mc = rg.Matches(tempLrc);
                al.Clear();
                //this.Text = "";
                foreach(Match m in mc){                        
                    string temp = m.Groups[0].Value;
                    //this.Text += temp + "+";                        
                    string mi = temp.Substring(temp.IndexOf('[') + 1, 2);
                    string se = temp.Substring(temp.IndexOf(':') + 1, 2);
                    string ms = temp.Substring(temp.IndexOf('.') + 1, 2);   
                    //this.Text += mi + ":" + se + "+";
                    string time = Convert.ToInt32(mi) * 60 + Convert.ToInt32(se) + "";  
                    al.Add(time);
                }
                //for (int i = 0; i < al.Count; i++)
                //{
                //    this.Text += al[i].ToString() + "+";
                //}
                tempLrc = rg.Replace(tempLrc, "\n");
                char[] remove = {'\r', '\n', ' '};
                this.richTextBox1.Text = tempLrc.TrimStart(remove);
                //}
            }
        }

        private void 显示歌词ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.显示歌词ToolStripMenuItem.Text == "显示桌面歌词")
            {
                this.lrcForm = new 透明歌词();
                this.tmForm = new 半透明(); 
                this.lrcForm.Owner = this;
                this.tmForm.Owner = this;
                this.tmForm.Hide();
                this.lrcForm.Show();                
                this.显示歌词ToolStripMenuItem.Text = "关闭桌面歌词";                
            }
            else {
                this.显示歌词ToolStripMenuItem.Text = "显示桌面歌词";                                                
                this.tmForm.Dispose();
                this.lrcForm.Dispose();
            }
        }

    
        public void setWord() 
        {
            this.显示歌词ToolStripMenuItem.Text = "显示桌面歌词";
            this.tmForm.Dispose();
        }

    
        public void moveTmform(Point pos)
        {
            this.tmForm.Location = pos;
        }

    
        public void setLrcformFocus()
        {
            this.lrcForm.TopMost = true;
        }

    
        public void showTmform(bool s)
        {
            //this.tmForm.TopMost = true;
            if (s)
            {
                this.tmForm.Show();
                if (this.first_in)
                {
                    this.lrcForm.TopMost = true;
                    this.first_in = false;
                }
            }
            else
            {
                this.first_in = true;
                this.tmForm.Hide();
            }            
        }

        public void addLocalSong(string[] files)
        {
            XmlDocument xd = new XmlDocument();
            xd.Load(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
            XmlElement xe = xd.DocumentElement;
            //string[] files = ofd.FileNames;
            int i = xe.ChildNodes.Count;
            foreach (string str in files)
            {
                i++;
                XmlNode x = xd.CreateElement("path");
                x.InnerText = str;
                xe.AppendChild(x);
                string[] t = { i.ToString(), Path.GetFileNameWithoutExtension(str) };
                ListViewItem lvi = new ListViewItem(t);
                this.listView1.Items.Add(lvi);
            }

            xd.Save(Directory.GetCurrentDirectory() + "\\data\\" + "music.xml");
        }
    }
}
